from django.db import models

from course.models import CourseModel
from user.models import UserModel
from utils.basemodel import BaseModel


# Create your models here.

class CouponModel(models.Model):
    name = models.CharField(max_length=50, verbose_name='优惠券名称')
    price = models.IntegerField(verbose_name='优惠券金额')
    start_time = models.DateTimeField(verbose_name='优惠券开始时间')
    end_time = models.DateTimeField(verbose_name='优惠券结束时间')
    condition = models.CharField(max_length=50, verbose_name='优惠券条件')
    number = models.IntegerField(verbose_name='优惠券数量', default=10, null=True)
    type = models.CharField(max_length=50, verbose_name='优惠券类型', default="满减券")

    class Meta:
        db_table = 'fgcoupon'

    def __str__(self):
        return self.name


class UserCoupon(BaseModel):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='用户')
    coupon = models.ForeignKey(CouponModel, on_delete=models.CASCADE, verbose_name='优惠券')

    class Meta:
        db_table = 'fgusercoupon'


class OrderModel(BaseModel):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='用户')
    order_no = models.CharField(max_length=50, verbose_name='订单号')
    total_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='总价')
    pay_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='实际支付价格')
    status = models.IntegerField(verbose_name='支付状态', choices=(
        (0, '待支付'),
        (1, '已支付'),
        (2, '已取消'),
        (3, '已完成'),
    ), default=0)
    score = models.IntegerField(verbose_name='积分', default=0)
    coupon = models.IntegerField(verbose_name='优惠券', default=0, null=True, blank=True)
    pay_type = models.IntegerField(verbose_name='支付方式', choices=(
        (1, '支付宝'),
        (2, '微信'),
        (3, '银行卡'),
    ), default=1)
    pay_time = models.DateTimeField(verbose_name='支付时间', null=True, blank=True)
    pay_no = models.CharField(max_length=50, verbose_name='支付流水号', null=True, blank=True)

    class Meta:
        db_table = 'fgorder'

    def __str__(self):
        return self.order_no


class OrderDetailModel(BaseModel):
    order = models.ForeignKey(OrderModel, on_delete=models.CASCADE, verbose_name='订单')
    course = models.ForeignKey(CourseModel, on_delete=models.CASCADE, verbose_name='课程')
    c_name = models.CharField(max_length=50, verbose_name='课程名称')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='价格')
    discount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='折扣价')
    cover = models.CharField(max_length=100, verbose_name='课程封面')

    class Meta:
        db_table = 'fgorderdetail'


class MyCourseModel(BaseModel):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='用户')
    course = models.ForeignKey(CourseModel, on_delete=models.CASCADE, verbose_name='课程')
    c_name = models.CharField(max_length=50, verbose_name='课程名称')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='价格')
    discount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='折扣价')
    cover = models.CharField(max_length=100, verbose_name='课程封面')

    class Meta:
        db_table = 'fgmycourse'
